using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace XProject
{
    public class TestScrollVierw : MonoBehaviour
    {

        private List<ResInfo> resInfos;
        public int testSize;
        public RectTransform m_item;
        public float m_offset;

        InfiniteSrollView m_infiniteSrollView = null;

        void Start()
        {
            resInfos = new List<ResInfo>();
            for (int i = 0; i < testSize; i++)
            {
                ResInfo t = new ResInfo();
                t.ResName = i.ToString();
                t.ResCode = (i * 1000 + 1).ToString();
                resInfos.Add(t);
            }


            m_infiniteSrollView = gameObject.AddComponent<InfiniteSrollView>();

            m_infiniteSrollView.Init(m_item, m_offset, OnItemRefresh);

            m_infiniteSrollView.RefreshContent(resInfos.Count);
        }

        private void OnItemRefresh(Transform transform, int index)
        {
            string str = resInfos[index].ResName + " |  " + resInfos[index].ResCode;
            transform.Find("Text").GetComponent<Text>().text = str;

            Button button = transform.GetComponent<Button>();
            button.onClick.RemoveAllListeners();
            button.onClick.AddListener(() => { OnClick(str); });
        }

        public void OnClick(string str)
        {
            XLog.LogGreen(str);
        }

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                int i = resInfos.Count;
                ResInfo t = new ResInfo();
                t.ResName = i.ToString();
                t.ResCode = (i * 1000 + 1).ToString();
                resInfos.Add(t);

                m_infiniteSrollView.RefreshContent(resInfos.Count);
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                resInfos.RemoveAt(resInfos.Count - 1);

                m_infiniteSrollView.RefreshContent(resInfos.Count);
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                resInfos.Clear();
                m_infiniteSrollView.Clear();
            }
        }
    }

}